﻿using System;
using System.IO;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;

using SBPweb.Modules.Smartparts;
using SBPweb.Modules.Modularity;

using SBPweb.MediaCenter.Tools.WF;

using SBPweb.MediaCenter.Modules.Customizer.Data;

namespace SBPweb.MediaCenter.Modules.Customizer.WF.Smartparts
{
    public partial class Login : UserControl, ISmartpart
    {

        #region Private fields

        private int pI = 0;
        private int pJ = 0;
        private bool pIsModal;
        private Aligning pAlign;
        private IModule pParentModule;
        private ProfilesDataSource pDataSource;

        protected Image pBBImage;

        #endregion

        #region Constructor logic

        public Login(IModule parentModule, bool isModal)
        {
            pIsModal = isModal;
            pParentModule = parentModule;
            InitializeComponent();
            Dock = System.Windows.Forms.DockStyle.Fill;
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.SupportsTransparentBackColor, true);

            Size = Screen.PrimaryScreen.Bounds.Size;

            this.SuspendLayout();

            Initialize();
            ResizeLoginWindow();

            this.ResumeLayout();

            pBBImage = new Bitmap(Size.Width, Size.Height);
            Graphics g = Graphics.FromImage(pBBImage);
            g.DrawImage(global::SBPweb.MediaCenter.Modules.Customizer.WF.Properties.Resources.LoginBackground01, this.ClientRectangle);
            Size s = global::SBPweb.MediaCenter.Modules.Customizer.WF.Properties.Resources.SBPweb_MediaCenter_Logo.Size;
            g.DrawImageUnscaled(global::SBPweb.MediaCenter.Modules.Customizer.WF.Properties.Resources.SBPweb_MediaCenter_Logo, new Rectangle(Size.Width / 2 - s.Width / 2, Size.Height - 80 - s.Height / 2, s.Width, s.Height));

        }

        #endregion

        #region Property accessors

        public Aligning Align
        {
            get
            {
                return pAlign;
            }
            set
            {
                pAlign = value;
                Dock = AligningConverter.AligningToDockStyle(value);
            }
        }

        public bool IsVisible
        {
            get
            {
                return base.Visible;
            }
            set
            {
                base.Visible = value;
            }
        }

        public bool IsEnabled
        {
            get
            {
                return base.Enabled;
            }
            set
            {
                base.Enabled = value;
            }
        }

        public bool IsModal
        {
            get
            {
                return pIsModal;
            }
        }

        public IModule ParentModule
        {
            get
            {
                return pParentModule;
            }
        }

        #endregion

        #region Public methods

        public void Initialize()
        {
            flpLoginButtons.ColumnCount = GetMaxColumns();
            flpLoginButtons.RowCount = 1;

            pDataSource = new ProfilesDataSource("Data\\Profiles.xml");

            foreach (Profiles.ProfileRow row in pDataSource.Profile)
            {
                AddNewButton(row);
            }
        }

        #endregion

        #region Private methods

        private void ResizeLoginWindow()
        {
            if (pDataSource == null || pDataSource.Profile.Length == 0)
            {
                return;
            }

            int width = (LoginAvatarButton.DefaultWidth + 6) * flpLoginButtons.ColumnCount;
            int height = (LoginAvatarButton.DefaultHeight + 6) * flpLoginButtons.RowCount;
            int w = (ClientSize.Width - width) / 2;
            int h = (ClientSize.Height - height) / 2;

            flpLoginButtons.Location = new Point(w, h);
            flpLoginButtons.Size = new Size(width, height);

        }

        private void AddNewButton(Profiles.ProfileRow row)
        {
            LoginAvatarButton button1 = new LoginAvatarButton();
            if (row.IsAvatarPathNull() || !File.Exists(row.AvatarPath))
            {
                if (row.Gender == "Female")
                {
                    button1.AvatarImage = Properties.Resources.UserIcon_Female;
                }
                else
                {
                    button1.AvatarImage = Properties.Resources.UserIcon_Male;
                }                
            }
            else
            {
                button1.AvatarImage = new Bitmap(row.AvatarPath);
            }

            button1.Text = row.Name;

            this.flpLoginButtons.Controls.Add(button1, pI++, pJ);
            if (pI >= flpLoginButtons.ColumnCount)
            {
                pI = 0;
                flpLoginButtons.RowCount++;
                pJ++;
            }


            /*
            button1.FlatAppearance.BorderSize = 0;
            button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            button1.Image = global::SBPweb.MediaCenter.Modules.Customizer.WF.Properties.Resources.Img011;
            button1.Location = new System.Drawing.Point(3, 3);
            button1.Name = "button1";
            button1.Size = new System.Drawing.Size(180, 180);
            button1.TabIndex = 0;
            button1.Text = userName;
            button1.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
            button1.UseVisualStyleBackColor = true;
            */
            button1.Click += new EventHandler(button1_Click);
        }

        private int GetMaxColumns()
        {
            return (int)(flpLoginButtons.ClientSize.Width / (LoginAvatarButton.DefaultWidth + 6));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            (this.Parent as Form).Close();
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            //base.OnPaintBackground(e);
            e.Graphics.DrawImageUnscaled(pBBImage, new Point(0, 0));
        }

        #endregion


        protected void Login_Resize(object sender, System.EventArgs e)
        {
            ResizeLoginWindow();
        }

    }
}
